package com.itheima.demo09JDBC案例;

import com.itheima.util.DataSourceUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 品牌数据的增删改查操作
 */
public class BrandTest {

    public static void main(String[] args) throws SQLException {
        // selectAll();
        // addBrand();
        // updateBrand();
        deleteBrand();
    }

    // 删除品牌数据
    public static void deleteBrand() throws SQLException {
        // 1.获取连接
        Connection conn = DataSourceUtils.getConnection();
        // 2.编写SQL语句
        String sql = "DELETE FROM tb_brand WHERE id=?;";
        // 3.执行SQL语句
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, 1);
        int row = pstmt.executeUpdate();
        System.out.println("row = " + row);

        // 4.释放资源
        pstmt.close();
        conn.close();
    }

    // 修改品牌数据
    public static void updateBrand() throws SQLException {
        // 1.获取连接
        Connection conn = DataSourceUtils.getConnection();
        // 2.编写SQL语句
        String sql = "UPDATE tb_brand SET brand_name=? WHERE id=?;";
        // 3.执行SQL语句
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, "红米");
        pstmt.setInt(2, 3);
        int row = pstmt.executeUpdate();
        System.out.println("row = " + row);

        // 4.释放资源
        pstmt.close();
        conn.close();
    }

    // 添加品牌数据
    public static void addBrand() throws SQLException {
        // 1.获取连接
        Connection conn = DataSourceUtils.getConnection();
        // 2.编写SQL语句
        String sql = "INSERT INTO tb_brand VALUES (NULL, ?, ?, ?, ?, ?);";
        // 3.执行SQL语句
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, "鸿星尔克");
        pstmt.setString(2, "福建鸿星尔克鞋业股份有限公司");
        pstmt.setInt(3, 1);
        pstmt.setString(4, "TO BE NO.1");
        pstmt.setByte(5, (byte)1);
        int row = pstmt.executeUpdate();
        System.out.println("row = " + row);

        // 4.释放资源
        pstmt.close();
        conn.close();
    }

    // 查询所有品牌数据
    public static void selectAll() throws SQLException {
        // 1.获取连接
        Connection conn = DataSourceUtils.getConnection();
        // 2.编写SQL语句
        String sql = "SELECT * FROM tb_brand;";
        // 3.执行SQL语句
        PreparedStatement pstmt = conn.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();

        List<Brand> list = new ArrayList<>();
        // 4.处理结果
        while (rs.next()) {
            // 取出这条数据
            int id = rs.getInt("id");
            String brandName = rs.getString("brand_name");
            String companyName = rs.getString("company_name");
            int ordered = rs.getInt("ordered");
            String description = rs.getString("description");
            byte status = rs.getByte("status");
            // 将一条记录保存到一个Brand对象中
            Brand brand = new Brand(id, brandName, companyName, ordered, description, status);
            // 将对象放到集合中
            list.add(brand);
        }
        // 5.释放资源
        rs.close();
        pstmt.close();
        conn.close();

        for (Brand brand : list) {
            System.out.println(brand);
        }
    }
}